﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

namespace WPF_Encrypt_Demo
{
    public class DataFileHelper
    {
        // 保存加密的点数据到文件
        public static void SavePointsToFile(string filePath, List<List<PointData>> allPoints)
        {
            using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
            using (GZipStream compressedStream = new GZipStream(fileStream, CompressionLevel.Optimal))
            using (BinaryWriter writer = new BinaryWriter(compressedStream))
            {
                writer.Write(allPoints.Count);

                for (int listIndex = 0; listIndex < allPoints.Count; listIndex++)
                {
                    var points = allPoints[listIndex];
                    writer.Write(points.Count);

                    foreach (var point in points)
                    {
                        byte[] pointBytes = point.ToBytes();
                        byte[] encryptedData = XorEncryptHelper.Encrypt(pointBytes);
                        writer.Write(encryptedData.Length); // 写入加密数据长度
                        writer.Write(encryptedData); // 写入加密数据
                    }
                }
            }
        }

        // 从文件读取并解密点数据
        public static List<List<PointData>> ReadPointsFromFile(string filePath)
        {
            List<List<PointData>> readPoints = new List<List<PointData>>();

            try
            {
                using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
                using (GZipStream compressedStream = new GZipStream(fileStream, CompressionMode.Decompress))
                using (BinaryReader reader = new BinaryReader(compressedStream))
                {
                    int listCount = reader.ReadInt32(); // 读取列表数量

                    for (int listIndex = 0; listIndex < listCount; listIndex++)
                    {
                        int pointCount = reader.ReadInt32(); // 读取当前列表中的点数量
                        List<PointData> currentList = new List<PointData>();

                        for (int i = 0; i < pointCount; i++)
                        {
                            int encryptedLength = reader.ReadInt32(); // 读取加密数据长度
                            byte[] encryptedData = reader.ReadBytes(encryptedLength); // 读取加密数据
                            byte[] decryptedBytes = XorEncryptHelper.Decrypt(encryptedData); // 解密数据
                            PointData point = PointData.FromBytes(decryptedBytes);
                            currentList.Add(point);
                        }
                        readPoints.Add(currentList);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"读取文件时出错: {ex.Message}");
            }

            return readPoints;
        }

        public static string GetFilePath(string baseDirectory, string measurementType)
        {
            // 创建以当前年月命名的文件夹
            string directoryPath = Path.Combine(baseDirectory, DateTime.Now.ToString("yyyy-MM"));

            // 确保文件夹存在
            Directory.CreateDirectory(directoryPath);

            // 构造文件名
            string fileName = $"{measurementType}_measurement_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.dat";
            return Path.Combine(directoryPath, fileName);
        }
    }
}